const mongoose = require('mongoose')
const config = require('./config')

module.exports = function(successCallback, errorCallback = () => {}) {
  mongoose.connect(`mongodb://${config.DBHOST}:${config.DBPOST}/${config.DBNAME}`).then(() => {
    console.log('准备使用')
    successCallback()
  }).catch(error => {
    console.log('初始链接错误', error)
    errorCallback()
  })

  mongoose.connection.on('error', error => {
    console.log('初始链接后错误', error)
    errorCallback()
  })

  mongoose.connection.on('disconnected', () => {
    console.log('失去连接错误')
    errorCallback()
  })
}